﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using DataLib.Common;
using DataLib.Accounts.Bus;

namespace Ewts.BaseData
{
    public partial class StoragePositionModify : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                dropBelong.DataSource = new DataLib.BLL.City().GetList("flag1='01'").Tables[0];
                dropBelong.DataTextField = "cityname";
                dropBelong.DataValueField = "citycode";
                dropBelong.DataBind();

                dropgraph.DataSource = new DataLib.BLL.City().GetList("flag1='02'").Tables[0];
                dropgraph.DataTextField = "cityname";
                dropgraph.DataValueField = "citycode";
                dropgraph.DataBind();

                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int id = (Convert.ToInt32(Request.Params["id"]));
                    ShowInfo(id);
                }
            }
        }

        private void ShowInfo(int id)
        {
            DataLib.BLL.StoragePostion bll = new DataLib.BLL.StoragePostion();
            DataLib.Model.StoragePostion model = bll.GetModel(id);
            if (model.alertvalume == null)
            {
                model.alertvalume = 0;
            }
            if (model.alertweight == null)
            {
                model.alertweight = 0;
            }
            this.lblid.Text = model.id.ToString();
            this.txtstoragecode.Text = model.storagecode;
            this.txtgoodstype.Text = model.goodstype;
            this.dropStorageType.SelectedValue = model.storagetype;
            this.txtmaxvalume.Text = model.maxvalume.ToString();
            this.txtalertvalume.Text = model.alertvalume.ToString();
            this.txtmaxweight.Text = model.maxweight.ToString();
            this.txtalertweight.Text = model.alertweight.ToString();
            this.txtvalumeunit.Text = model.valumeunit;
            dropBelong.SelectedItem.Text = model.belongCity;
            dropgraph.SelectedItem.Text = model.graphicCity;

        }

        public void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (this.txtstoragecode.Text.Trim().Length == 0)
            {
                strErr += "库位代码不能为空！\\n";
            }
            if (txtmaxvalume.Text != "")
            {
                if (!PageValidate.IsNumber(txtmaxvalume.Text))
                {
                    strErr += "最大容量必须为整数\\n";
                }
            }
            if (txtalertvalume.Text != "")
            {
                if (!PageValidate.IsNumber(txtalertvalume.Text))
                {
                    strErr += "警告容量必须为整数\\n";
                }
            }
            if (txtmaxvalume.Text != "" && txtalertvalume.Text != "")
            {
                if (decimal.Parse(txtmaxvalume.Text) < decimal.Parse(txtalertvalume.Text))
                {
                    strErr += "最大容量不能小于警告容量\\n";
                }
            }
            if (txtmaxweight.Text != "" && txtalertweight.Text != "")
            {
                if (decimal.Parse(txtmaxweight.Text) < decimal.Parse(txtalertweight.Text))
                {
                    strErr += "最大重量不能小于警告重量\\n";
                }
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            int id = int.Parse(this.lblid.Text);
            string storagecode = this.txtstoragecode.Text;
            string goodstype = this.txtgoodstype.Text;
            string storagetype = this.dropStorageType.SelectedValue;
            int maxvalume = int.Parse(this.txtmaxvalume.Text);
            int alertvalume = int.Parse(this.txtalertvalume.Text);
            decimal maxweight = decimal.Parse(this.txtmaxweight.Text);
            decimal alertweight = decimal.Parse(this.txtalertweight.Text);
            string valumeunit = this.txtvalumeunit.Text;


            DataLib.Model.StoragePostion model = new DataLib.Model.StoragePostion();
            model.id = id;
            model.storagecode = storagecode;
            model.goodstype = goodstype;
            model.storagetype = storagetype;
            model.maxvalume = maxvalume;
            model.alertvalume = alertvalume;
            model.maxweight = maxweight;
            model.alertweight = alertweight;
            model.valumeunit = valumeunit;
            model.belongCity = dropBelong.SelectedItem.Text;
            model.graphicCity = dropgraph.SelectedItem.Text;
            DataLib.BLL.StoragePostion bll = new DataLib.BLL.StoragePostion();
            bll.Update(model);
            DataLib.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "StoragePositionList.aspx");

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("StoragePositionList.aspx");
        }
    }
}